#include "BangoAgent.h"

BangoLogger BangoAgent::_bangoLogger;
void BangoAgent::UserAgent(QString userAgent){ _bangoLogger.UserAgent(userAgent); }
void BangoAgent::setLogEnabled(bool logEvents){ _bangoLogger.LogEvents(logEvents); }
bool BangoAgent::getLogEnabled() { return _bangoLogger.LogEvents(); }

void BangoAgent::setContinueSessionSeconds(int continueSessionSeconds)
{
    _bangoLogger.SessionResumePeriod(continueSessionSeconds);
}
int BangoAgent::getContinueSessionSeconds()
{
    return _bangoLogger.SessionResumePeriod();
}

void BangoAgent::setUseOfflineStorage(bool useOfflineStorage)
{
    _bangoLogger.UseOfflineStorage(useOfflineStorage);
}
bool BangoAgent::getUseOfflineStorage()
{
    return _bangoLogger.UseOfflineStorage();
}

void BangoAgent::setApplicationVersion(QString applicationVersion)
{
    _bangoLogger.ApplicationVersion(applicationVersion);
}
QString BangoAgent::getApplicationVersion()
{
    return _bangoLogger.ApplicationVersion();
}

void BangoAgent::setUserId(QString userId)
{
    _bangoLogger.UserID(userId);
}
QString BangoAgent::getUserId()
{
    return _bangoLogger.UserID();
}

void BangoAgent::setPostURL(QString postURL)
{
    _bangoLogger.PostURL(postURL);
}
QString BangoAgent::getPostURL()
{
    return _bangoLogger.PostURL();
}

void BangoAgent::setApplicationType(QString applicationType)
{
    _bangoLogger.ApplicationType(applicationType);
}
QString BangoAgent::getApplicationType()
{
    return _bangoLogger.ApplicationType();
}

void BangoAgent::setApplicationChannel(QString applicationChannel)
{
    _bangoLogger.ApplicationChannel(applicationChannel);
}
QString BangoAgent::getApplicationChannel()
{
    return _bangoLogger.ApplicationChannel();
}

void BangoAgent::setFindLocation(bool findLocation)
{
    _bangoLogger.FindLocation(findLocation);
}
bool BangoAgent::getFindLocation()
{
    return _bangoLogger.FindLocation();
}
void BangoAgent::startSession(QString applicationKey, QString eventValue, QString eventDetail, QMap<QString, QString> eventParameters)
{
    _bangoLogger.InitLogger(applicationKey);
    _bangoLogger.LogEventStart(eventValue, eventDetail, eventParameters);
    PostStoredEventsNow();
}

void BangoAgent::onStartSession(QString applicationKey, QString eventValue, QString eventDetail, QMap <QString, QString> eventParameters)
{
    if (applicationKey == "")
    {
        throw "Api key not specified";
    }

    if (_bangoLogger.UserAgent() == "")
    {
        _bangoLogger.UserAgent(BangoStatic::GetUserAgent());
    }

    try
    {
        startSession(applicationKey, eventValue, eventDetail, eventParameters);
    }
    catch (exception& ex)
    {
        _bangoLogger.LogInternalErrorCritical(ex);
    }
}

void BangoAgent::onStartSession(QString applicationKey, QMap<QString, QString> eventParameters)
{
    onStartSession(applicationKey, "", "", eventParameters);
}

void BangoAgent::onStartSession(QString applicationKey, QString eventValue, QString eventDetail)
{
    onStartSession(applicationKey, eventValue, eventDetail, QMap<QString, QString>() );
}

void BangoAgent::onStartSession(QString applicationKey)
{
    onStartSession(applicationKey, "", "");
}

void BangoAgent::endSession(bool forceEventPosting, QString eventValue, QString eventDetail, QMap<QString, QString> eventParameters)
{
    _bangoLogger.LogEventEnd(eventValue, eventDetail, eventParameters);
    bool saveEventsSuccessful = true;

    if (!forceEventPosting)
    {
        //Attempt to save events. If unsuccessful, attempt to post.
        if (!_bangoLogger.SaveCurrentEvents())
        {
            saveEventsSuccessful = false;
        }
    }

    if (forceEventPosting || !saveEventsSuccessful)
    {
        PostEventsNow(true, false);
    }
}

void BangoAgent::onEndSession(bool forceEventPosting, QString eventValue, QString eventDetail, QMap<QString, QString> eventParameters)
{
    try
    {
        endSession(forceEventPosting, eventValue, eventDetail, eventParameters);
    }
    catch (exception& ex)
    {
    }
}

void BangoAgent::onEndSession(bool forceEventPosting, QMap<QString, QString> eventParameters)
{
    onEndSession(forceEventPosting, "", "", eventParameters);
}

void BangoAgent::onEndSession(bool forceEventPosting, QString eventValue, QString eventDetail)
{
    onEndSession(forceEventPosting, eventValue, eventDetail, QMap<QString, QString>());
}

void BangoAgent::onEndSession(bool forceEventPosting)
{
    onEndSession(forceEventPosting, QMap<QString, QString>());
}

void BangoAgent::onEndSession()
{
    onEndSession(false);
}

void BangoAgent::onEvent(QString eventName, QString eventValue, QString eventDetail, QMap<QString, QString> eventParameters)
{
    try
    {
        _bangoLogger.LogEvent(eventName, eventValue, eventDetail, eventParameters);
    }
    catch (exception& ex)
    {
    }
}

void BangoAgent::onEvent(QString eventName, QString eventValue)
{
    onEvent(eventName, eventValue, "", QMap<QString,QString>());
}

void BangoAgent::onEvent(QString eventName, QString eventValue, QString eventDetail)
{
    onEvent(eventName, eventValue, eventDetail, QMap<QString,QString>());
}

void BangoAgent::onEvent(QString eventName, QString eventValue, QMap<QString, QString> eventParameters)
{
    onEvent(eventName, eventValue, " ", eventParameters);
}

void BangoAgent::onEvent(QString eventName)
{
    onEvent(eventName, "", "", QMap<QString,QString>());
}

void BangoAgent::onDemandVideoStart(QString eventValue, QString eventDetail, QMap<QString, QString> eventParameters)
{
    try
    {
        eventParameters[BangoConstants::VIDEO_PARAMETERNAME] = BangoConstants::VIDEO_ONDEMANDPARAMETER;
        _bangoLogger.LogEvent(BangoSessionEvents::EVENT_VIDEOSTART, eventValue, eventDetail, eventParameters);
    }
    catch (exception& ex)
    {
    }
}
void BangoAgent::onDemandVideoStart(QString eventValue, QString eventDetail){
    onDemandVideoStart(eventValue, eventDetail, QMap<QString,QString>());
}
void BangoAgent::onDemandVideoStart(QString eventValue, QMap<QString, QString> eventParameters){
    onDemandVideoStart(eventValue, "", eventParameters);
}
void BangoAgent::onDemandVideoStart(QString eventValue){
    onDemandVideoStart(eventValue, "");
}
void BangoAgent::onDemandVideoStart(){
    onDemandVideoStart("");
}


void BangoAgent::onDemandVideoStop(QString eventValue, QString eventDetail, QMap<QString, QString> eventParameters)
{
    try
    {
        eventParameters[BangoConstants::VIDEO_PARAMETERNAME] = BangoConstants::VIDEO_ONDEMANDPARAMETER;
        _bangoLogger.LogEvent(BangoSessionEvents::EVENT_VIDEOSTOP, eventValue, eventDetail, eventParameters);
    }
    catch (exception& ex)
    {
    }
}

void BangoAgent::onDemandVideoStop(QString eventValue, QString eventDetail)
{
    onDemandVideoStop(eventValue, eventDetail, QMap<QString,QString>());
}
void BangoAgent::onDemandVideoStop(QString eventValue, QMap<QString, QString> eventParameters)
{
    onDemandVideoStop(eventValue, "", eventParameters);
}
void BangoAgent::onDemandVideoStop(QString eventValue)
{
    onDemandVideoStop(eventValue, "");
}
void BangoAgent::onDemandVideoStop()
{
    onDemandVideoStop("");
}


void BangoAgent::onLiveVideoStart(QString eventValue, QString eventDetail, QMap<QString, QString> eventParameters)
{
    try{
        eventParameters[BangoConstants::VIDEO_PARAMETERNAME] = BangoConstants::VIDEO_LIVEPARAMETER;
        _bangoLogger.LogEvent(BangoSessionEvents::EVENT_VIDEOSTART, eventValue, eventDetail, eventParameters);
    }
    catch (exception& ex)
    {
    }
}
void BangoAgent::onLiveVideoStart(QString eventValue, QString eventDetail)
{
    onLiveVideoStart(eventValue, eventDetail, QMap<QString,QString>());
}
void BangoAgent::onLiveVideoStart(QString eventValue, QMap<QString, QString> eventParameters)
{
    onLiveVideoStart(eventValue, "", eventParameters);
}
void BangoAgent::onLiveVideoStart(QString eventValue)
{
    onLiveVideoStart(eventValue, "");
}
void BangoAgent::onLiveVideoStart()
{
    onLiveVideoStart("");
}

void BangoAgent::onLiveVideoStop(QString eventValue, QString eventDetail, QMap<QString, QString> eventParameters)
{
    try
    {
        eventParameters[BangoConstants::VIDEO_PARAMETERNAME] = BangoConstants::VIDEO_LIVEPARAMETER;
        _bangoLogger.LogEvent(BangoSessionEvents::EVENT_VIDEOSTOP, eventValue, eventDetail, eventParameters);
    }
    catch (exception& ex)
    {
    }
}

void BangoAgent::onLiveVideoStop(QString eventValue, QString eventDetail)
{
    onLiveVideoStop(eventValue, eventDetail, QMap<QString,QString>());
}
void BangoAgent::onLiveVideoStop(QString eventValue, QMap<QString, QString> eventParameters)
{
    onLiveVideoStop(eventValue, "", eventParameters);
}
void BangoAgent::onLiveVideoStop(QString eventValue)
{
    onLiveVideoStop(eventValue, "");
}
void BangoAgent::onLiveVideoStop()
{
    onLiveVideoStop("");
}

void BangoAgent::onIdle(QString eventValue, QString eventDetail, QMap<QString, QString> eventParameters)
{
    try
    {
        _bangoLogger.LogEvent(BangoSessionEvents::SESSION_IDLE_START, eventValue, eventDetail, eventParameters);
    }
    catch (exception& ex)
    {
    }
}

void BangoAgent::onIdle(QString eventValue, QString eventDetail)
{
    onIdle(eventValue, eventDetail, QMap<QString,QString>());
}
void BangoAgent::onIdle(QString eventValue, QMap<QString, QString> eventParameters)
{
    onIdle(eventValue, "", eventParameters);
}
void BangoAgent::onIdle(QString eventValue)
{
    onIdle(eventValue, "");
}
void BangoAgent::onIdle()
{
    onIdle("");
}

void BangoAgent::onResume(QString eventValue, QString eventDetail, QMap<QString, QString> eventParameters)
{
    try
    {
        _bangoLogger.LogEvent(BangoSessionEvents::SESSION_IDLE_END, eventValue, eventDetail, eventParameters);
    }
    catch (exception& ex)
    {
    }
}

void BangoAgent::onResume(QString eventValue, QString eventDetail)
{
    onResume(eventValue, eventDetail, QMap<QString,QString>());
}
void BangoAgent::onResume(QString eventValue, QMap<QString, QString> eventParameters)
{
    onResume(eventValue, "", eventParameters);
}
void BangoAgent::onResume(QString eventValue)
{
    onResume(eventValue, "");
}
void BangoAgent::onResume()
{
    onResume("");
}

void BangoAgent::onPageView(QString eventValue, QString eventDetail, QMap<QString, QString> eventParameters)
{
    try
    {
        _bangoLogger.LogEvent(BangoSessionEvents::EVENT_PAGEVIEW, eventValue, eventDetail, eventParameters);
    }
    catch (exception& ex)
    {
    }
}

void BangoAgent::onPageView(QString eventValue, QString eventDetail)
{
    onPageView(eventValue, eventDetail, QMap<QString,QString>());
}
void BangoAgent::onPageView(QString eventValue, QMap<QString, QString> eventParameters)
{
    onPageView(eventValue, "", eventParameters);
}
void BangoAgent::onPageView(QString eventValue)
{
    onPageView(eventValue, "");
}
void BangoAgent::onPageView()
{
    onPageView("");
}

void BangoAgent::onSearch(QString eventValue, QString eventDetail, QMap<QString, QString> eventParameters)
{
    try
    {
        _bangoLogger.LogEvent(BangoSessionEvents::EVENT_SEARCH, eventValue, eventDetail, eventParameters);
    }
    catch (exception& ex)
    {
    }
}

void BangoAgent::onSearch(QString eventValue, QString eventDetail)
{
    onSearch(eventValue, eventDetail, QMap<QString,QString>());
}
void BangoAgent::onSearch(QString eventValue, QMap<QString, QString> eventParameters)
{
    onSearch(eventValue, "", eventParameters);
}
void BangoAgent::onSearch(QString eventValue)
{
    onSearch(eventValue, "");
}
void BangoAgent::onSearch()
{
    onSearch("");
}

void BangoAgent::onError(QString errorId, QString message, QString errorClass, QMap<QString, QString> errorParameters)
{
    try
    {
        _bangoLogger.LogEventError(errorId, message, errorClass, errorParameters);
    }
    catch (exception& ex)
    {
        _bangoLogger.LogInternalErrorCritical(ex);
    }
}

void BangoAgent::onError(QString errorId, QString message, QString errorClass)
{
    onError(errorId, message, errorClass, QMap<QString, QString>());
}

void BangoAgent::onError(QString errorId, QMap<QString, QString> errorParameters)
{
    onError(errorId, "", "", errorParameters);
}

void BangoAgent::onError(QString errorId)
{
    onError(errorId, QMap<QString, QString>());
}

void BangoAgent::onError()
{
    onError("");
}

void BangoAgent::PostEvents()
{
    PostEventsNow(true, true);
}

void BangoAgent::PostStoredEventsNow()
{
    PostEventsNow(false, true);
}

void BangoAgent::PostEventsNow(bool postCurrent, bool useThread)
{
    if (useThread)
    {
        BangoPoster * bPoster = new BangoPoster(&_bangoLogger, postCurrent);
        bPoster->start();
    }
    else
    {
        _bangoLogger.PostStoredEvents();
        if (postCurrent)
        {
            _bangoLogger.PostCurrentEvents();
        }
    }
}

QString BangoAgent::ViewBatch()
{
    return _bangoLogger.ViewBatch();
}
